/**
 * 作者: 会写BUG
 * 创作日期: 2025/3/31 17:42
 * 描述: cwbQtPractice.MenuPanel
 *
 **/
#include "menu_panel.h"

#include <QGridLayout>
#include <QPainter>

MenuPanel::MenuPanel(QWidget* _parent)
{
    QStringList menu_items = {
        "关于", "语言", "重置", "退出",
        "界面", "设置", "速度", "返回"
    };

    QGridLayout * layout = new QGridLayout(this);

    int row = -1, col = 0;
    for (int index = 0; index < menu_items.count(); ++index) {
        auto menu_item = new GameButton(menu_items[index]);
        menu_item->setFont(QFont("Arial", 16, QFont::DemiBold));
        menu_item->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

        if (col % 4 == 0) {
            row++;
            col = 0;
        }

        layout->addWidget(menu_item, row, col++);
        connect(menu_item, &GameButton::clicked, this, &MenuPanel::buttonClicked);
    }

}

MenuPanel::~MenuPanel()
{
}

void MenuPanel::buttonClicked()
{
    GameButton * button = qobject_cast<GameButton*>(sender());
    if (button == nullptr) {
        return;
    }
    if (button->text() == "关于") {
        Q_EMIT aboutGame();
    }

}

void MenuPanel::paintEvent(QPaintEvent* event)
{
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);

    painter.setPen(Qt::transparent);
    painter.setBrush(QColor(227, 174, 106));
    painter.drawRect(rect());
}
